﻿using System;
using System.Collections.Specialized;
using System.Web;
using log4net;

namespace SpeakOut.Lib.Web
{
    public class PostQueryString
    {
        private static readonly ILog _log = LogManager.GetLogger(typeof(PostQueryString));

        public string Permalink { get; private set; }

        public DateTime PublishDate { get; private set; }

        /// <summary>
        /// ctor
        /// </summary>
        /// <param name="request"></param>
        public PostQueryString(HttpRequest request)
        {
            try
            {
                parseQueryString(request.QueryString);
            }
            catch (Exception e)
            {
                _log.Error("Error parsing QueryString",e);
                throw;
            }
        }

        private void parseQueryString(NameValueCollection collection)
        {
            Permalink = collection["permalink"].Replace("/", "");

            int year = collection["year"].ToInt();
            int month = collection["month"].ToInt();
            int day = collection["day"].ToInt();
            PublishDate = new DateTime(year, month, day);
        }

        public override string ToString()
        {
            return string.Format(@"~/{0}/{1:00}/{2:00}/{3}", PublishDate.Year, PublishDate.Month,
                                 PublishDate.Day, Permalink);
        }
    }
}
